package cn.chencq.proxy.timer.handler;

import java.net.Proxy;
import java.util.HashSet;
import java.util.Set;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.chencq.proxy.constants.RedisKey;
import cn.chencq.proxy.util.Util;

import com.limei.adplatform.redis.cluster.RedisClusterFactory;

public class GeneralProxyHandler implements ProxyHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(GeneralProxyHandler.class);

	/**
	 * 获取处理器
	 * 
	 * @return
	 */
	@Override
	public String getHandler() {
		return "normal";
	}

	/**
	 * 请求网址
	 * 
	 * @param url
	 * @return
	 */
	@Override
	public Document requestDoc(String rid, String url, Proxy proxy) {
		Connection con = Jsoup.connect(url);
		if (proxy != null) {
			con.proxy(proxy);
		}

		String host = Util.getHost(url);
		con.header("Host", host);

		con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0");
		Document doc = null;
		try {
			doc = con.ignoreContentType(true).timeout(4000).get();
		} catch (Exception e) {
			LOGGER.error("rid:{},requestDoc throw exception", rid, e);
		}
		return doc;
	}

	/**
	 * 请求网址
	 * 
	 * @param url
	 * @return
	 */
	@Override
	public Response request(String rid, String url, Proxy proxy) {
		Connection con = Jsoup.connect(url);
		if (proxy != null) {
			con.proxy(proxy);
		}
		String host = Util.getHost(url);
		con.header("Host", host);

		con.header("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.1599.103 Mobile Safari/537.36");
		Response res = null;
		try {
			res = con.ignoreContentType(true).timeout(5000).method(Method.GET).execute();
		} catch (Exception e) {
			LOGGER.error("rid:{},request throw exception", rid, e);
		}
		return res;
	}

	@Override
	public int parseResp(String rid, Document doc) {
		Set<String> p = new HashSet<String>();
		try {
			if (doc != null) {
				String body = doc.html();
				p.add(body);
				putToCache(rid, p);
			}
		} catch (Exception e) {
			LOGGER.error("rid:{},parseResp throw exception", rid, e);
		}
		return p.size();
	}

	@Override
	public int parseResp(String rid, Response res) {
		Set<String> p = new HashSet<String>();
		try {
			if (res != null) {
				String body = res.body();
				p.add(body);
				putToCache(rid, p);
			}
		} catch (Exception e) {
			LOGGER.error("rid:{},parseResp throw exception", rid, e);
		}
		return p.size();
	}

	@Override
	public int crawler(String rid, String url, Proxy proxy) {
		int ret = 0;
		try {
			Response res = request(rid, url, proxy);
			if (res != null) {
				ret = parseResp(rid, res);
			} else {
				ret = 0;
			}
		} catch (Exception e) {
			ret = 0;
		}
		return ret;
	}

	@Override
	public void putToCache(String rid, Set<String> set) {
		boolean flag = false;
		if (set != null && set.size() > 0) {
			String[] memebers = Util.setToArray(set);
			
			String clusterName = RedisKey.getClusterName();
			//添加到带校验的队列中
			RedisClusterFactory.rpush(clusterName, RedisKey.getProxyQueueKey(), memebers);
			
			//添加当天获取的代理集合中
			flag = RedisClusterFactory.sadd(clusterName, 
					RedisKey.getDayProxyKey(), memebers) == 1 ? true : false;
		}
		LOGGER.info("rid:{},put proxy to cache,success:{}", rid, flag);
	}
}
